package com.is.servlet;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.is.util.ParamUtil;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.teetaa.util.*;

public class ZoomImage extends HttpServlet
{
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
	    
        int imageWidth = ParamUtil.getInt(request, "txt_width",150);
        if(imageWidth<=0){imageWidth=150;}
        int imageHeight = ParamUtil.getInt(request, "txt_height",100);
        if(imageHeight<=0){imageHeight = 130;}
        int cutTop = ParamUtil.getInt(request,"txt_top",2);
        if(cutTop<=0){cutTop=2;}
		int cutLeft = ParamUtil.getInt(request,"txt_left",15);
		if(cutLeft<0){ cutLeft=15;}
		int dropWidth = ParamUtil.getInt(request,"txt_DropWidth",120);
		int dropHeight = ParamUtil.getInt(request,"txt_DropHeight",120);
		float imageZoom = Float.parseFloat(request.getParameter("txt_Zoom"));
		String picture = request.getParameter("picture");
		
//		System.out.println("imageWidth : "+imageWidth);
//		System.out.println("imageHeight : "+imageHeight);
//		System.out.println("cutTop : "+cutTop);
//		System.out.println("cutLeft : "+cutLeft);
//		System.out.println("dropWidth : "+dropWidth);
//		System.out.println("dropHeight : "+dropHeight);
//		System.out.println("imageZoom : "+imageZoom);
//		System.out.println("picture : "+picture);
//		System.out.println("url :"+request.getRealPath("")+"/UploadPhoto/"+picture);
		Rectangle rec = new Rectangle(cutLeft,cutTop,dropWidth,dropHeight);
		File file = new File(request.getRealPath("")+"/User/UserHeadImage/"+picture);
		//BufferedImage image = ImageIO.read(new File(request.getRealPath("")+"/UploadPhoto/"+picture));  
		saveSubImage(new File(request.getRealPath("")+"/UploadPhoto/"+picture),file,imageWidth,imageHeight,rec);
		response.sendRedirect("/InterStu/uploadimage.jsp?Picurl="+picture+"&step=3");
	}
	
	private static void saveSubImage(File srcImageFile, File descDir,int width,int height,Rectangle rect) throws IOException   
	{   

		ImageHepler.cut(srcImageFile, descDir, width, height, rect);
	} 

}
